package gim_client

import (
	"fmt"
	"github.com/spf13/viper"
)

type Config struct {
	GimBroker struct {
		Endpoint       string `yaml:"endpoint"`
		ConnectTimeOut int32  `yaml:"connectTimeOut"`
	} `yaml:"gimBroker"`
	Http struct {
		Port int `yaml:"port"`
	} `yaml:"http"`
	Log struct {
		FilePath string `yaml:"filePath"`
	} `yaml:"log"`
}

func InitConfig(path string) (config *Config, err error) {

	viper.SetConfigType("yaml")
	viper.SetConfigFile(path)
	err = viper.ReadInConfig()
	if err != nil {
		fmt.Printf("Failed to read config file: %v", err)
		return nil, err
	}
	err = viper.Unmarshal(&config)
	if err != nil {
		fmt.Printf("Failed to unmarshal analysis_rules: %v", err)
		return nil, err
	}
	if config == nil {
		return nil, fmt.Errorf("config is nil")
	}
	return
}
